angular.module('UCX.Cmis')
.directive('breadCrumb',(CmisService, localStorageService, ModelService,$window,$timeout,$rootScope)->
  templatePath='template-path/'
  scope:{
    folderTree:'=',
    dbClick:'=',
    chosedFileDocument:'&',
    choseFile:'=',
    currentFolder:'='
  }
  restrict:'E'
  replace:true
  templateUrl:templatePath+'breadcrumb.html'
  link:(scope, element)->
    scope.timer=null
    scope.hideIndex=0
    childrenCount=0
    totalWidth=0
    eachWidth=0
    eachWidthNormal=0
    showNum=0
    isFile=false
    #      readjust函数 就是 对目录进行重新修正的函数
    readjust=()->
      MIN_WIDTH=60
      #        由于动态 获取不到新生成元素的属性 所以这个地方使用 timeout
      #           1.childrenCount 子元素数量 int
      #           2.totalWidth    显示区宽度 int
      #           3.showNum       最多可以显示几个目录 int
      #           4.MIN_WIDTH     设置的最小宽度 常数 int
      #           5.eachWidth     有隐藏元素时候的宽度 int
      #           5.eachWidthNormal    无隐藏元素时候的宽度 int
      #        140为 右侧辅助栏宽度 46为 / 宽度  40 为 padding值
#     detect ie10
      if (navigator.appVersion.indexOf("MSIE 10") != -1)
#        window.alert('This is IE 10')
        IE10=true
      else
        IE10=false
#     detect ie11
      UAString = navigator.userAgent
      if (UAString.indexOf("Trident") != -1 && UAString.indexOf("rv:11") != -1)
#        window.alert('This is IE 11')
        IE11=true
      else
        IE11=false
      if IE11 || IE10
        IE = true
      else
        IE = false
      childrenCount =element[0].childElementCount
      if childrenCount < 2
        return false
#      if
#        elem.css('white-space','normal')
      #      console.log('lastChild',$($(element).find('li')[childrenCount-1]).find('a').width())
      totalChildrenWidth=0
      for i in [0..childrenCount-1]
        if IE
          $($(element).find('li')[i]).css('white-space','normal')
          $($(element).find('li')[i]).css('max-width','100%')
#       等待 元素 加载完毕 计算他的长度
        setTimeout((elem)->
          totalChildrenWidth += elem.children().width() + 32
          if IE
            elem.css('white-space','nowrap')
            $($(element).css('max-width','auto'))
        ,200,$($(element).find('li')[i]))
      setTimeout(()->
        totalWidth=$('.ba-col-m').width()
#        console.log('totalWidth',totalWidth)
#        console.log('totalChildrenWidth',totalChildrenWidth)
        if totalChildrenWidth<totalWidth
          for i in element[0].children
            $(i).css({'max-width':'100%'})
            $(i).addClass('breadcrumb-animate')
        else
          isFile=false
          showNum=Math.floor(totalWidth/MIN_WIDTH)
          eachWidth=Math.floor(totalWidth/showNum)
          eachWidthNormal=Math.floor(totalWidth/childrenCount)
  #     console.log(childrenCount)
          for i in element[0].children
            if Math.floor(totalWidth/childrenCount)<60
  #          scope.hideIndex= if childrenCount-Math.floor(totalWidth/60)<0 then 0 else childrenCount-Math.floor(totalWidth/60)
            else
              scope.hideIndex=0
              $(i).css({'max-width':eachWidthNormal+'px'})
              $(i).addClass('breadcrumb-animate')
          if scope.timer
            clearTimeout(scope.timer)
      ,220)

    #监测是否有新目录
    scope.$watch(
      ()->
        if $rootScope.currentFolder
          $rootScope.currentFolder.path
        else
          $rootScope.currentFolder
    ,
      (newValue)->
        if scope.timer
          clearTimeout(scope.timer)
        scope.timer=setTimeout(
          ()->
            readjust()
        ,300)
    ,true)

    #现在 监测的是这个 ul宽度的变化 绑定 resize 事件才能触发变化
    $($window).bind('resize',()->
      timer = $timeout ()->
        scope.$apply()
      , 50
      scope.$on('$destroy', ()->
        $timeout.cancel(timer)
      )
    )
    scope.$watch(
      ()->
        $('.ba-col-m') && $('.ba-col-m') && $('.ba-col-m').width()
    ,
      (newValue)->
        if scope.timer
          clearTimeout(scope.timer)
        scope.timer=setTimeout(
          ()->
            readjust()
        ,150)
    ,true)
)
